package com.station;

import java.util.ArrayList;

/**
 * @author SnowmanAndBee
 * @version 1.0
 * 天气数据 被观察的对象
 * @date 2021/12/14 - 19:44
 */
public class WeatherData implements Subject{
    private ArrayList<Observer> observers;
    private double temperature;
    private double humidity;
    private double pressure;

    public WeatherData() {
        observers = new ArrayList<>();
    }

    @Override
    public void registerObserver(Observer o) {
        observers.add(o);
    }

    @Override
    public void removeObserver(Observer o) {
        int i = observers.indexOf(o);
        if (i >= 0) {
            observers.remove(i);
        }
    }

    @Override
    public void notifyObservers() {
        for (Observer observer : observers) {
            observer.update(temperature, humidity, pressure);
        }
    }

    /**
     * 气象台得到更新的观测值时，通知注册的观察者
     */
    public void measurementsChanged() {
        notifyObservers();
    }

    /**
     * 外部获取更新的天气数据信息
     * @param temperature 气温
     * @param humidity 湿度
     * @param pressure 气压
     */
    public void setMeasurements(double temperature, double humidity, double pressure) {
        this.temperature = temperature;
        this.humidity = humidity;
        this.pressure = pressure;
        measurementsChanged();
    }
}
